package yxy.apple.util;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ActiveUtil {

	final static public long SECOND = 1000;
	final static public long MINUTE = 60 * SECOND;
	final static public long HOUR = 60 * MINUTE;
	final static public long DAY = 24 * HOUR;

	public static Date getDate(long time) {
		Date date = new Date(time);
		return date;
	}

	public static Date getDelayDate(long delay) {
		long time = System.currentTimeMillis() + delay;
		Date date = new Date(time);
		return date;
	}

	public static Date getNowDate() {
		long time = System.currentTimeMillis();
		Date date = new Date(time);
		return date;
	}

	public static DateTime getStartDayTime(Date date) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String str = format.format(date);
		return DateTime.parse(str);
	}

	public static int getBetweenHours(long time1, long time2) {
		DateTime dt1 = new DateTime(time1);

		DateTime dt2 = new DateTime(time2);

		Period p = new Period(dt1, dt2, PeriodType.hours());
		return p.getHours();
	}

	/**
	 * 获得两天的所差天数
	 * 
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static int getBetweenDays(long time1, long time2) {
		DateTime dt1 = new DateTime(time1);
		DateTime temp1 = dt1.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);

		DateTime dt2 = new DateTime(time2);
		DateTime temp2 = dt2.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);

		Period p = new Period(temp1, temp2, PeriodType.days());
		return p.getDays();
	}
	
	public static int getBetweenDays(DateTime dt1, DateTime dt2) {
		DateTime temp1 = dt1.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
		DateTime temp2 = dt2.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);

		Period p = new Period(temp1, temp2, PeriodType.days());
		return p.getDays();
	}

	public static int getBetweenDays(Date time1, Date time2) {
		return getBetweenDays(time1.getTime(), time2.getTime());
	}

	/** 返回某月的天数 一月是0 */
	public static int getDaysInMonth(int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.MONTH, month + 1);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.get(Calendar.DATE);
	}

	/** 返回某年某月的天数 一月是0 */
	public static int getDaysInMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month + 1);
		cal.set(Calendar.DATE, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.get(Calendar.DATE);
	}

	public static int days(DateTime dt1, DateTime dt2) {
		DateTime dt1temp = dt1.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
		DateTime dt2temp = dt2.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);

		return Days.daysBetween(dt1temp, dt2temp).getDays();
	}
	
	public static int days0(DateTime dt1, DateTime dt2) {
		DateTime dt1temp = dt1.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
//		DateTime dt2temp = dt2.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);

		return Days.daysBetween(dt1temp, dt2).getDays();
	}
	
	public static int daysNow(DateTime dt1) {
		return days(dt1, DateTime.now());
	}
	
	/**
	 * 获取当天0点
	 * @return
	 */
	public static DateTime time0() {
		return DateTime.now().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}
	
	public static DateTime time2(DateTime time) {
		return time.withHourOfDay(2).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time5(DateTime time) {
		return time.withHourOfDay(5).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	
	public static DateTime time(DateTime time, int hours) {
		return time.withHourOfDay(hours).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}
	
	public static DateTime time24(DateTime time) {
		return time.withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).withMillisOfSecond(0);
	}

	public static DateTime time23_30(DateTime time) {
		return time.withHourOfDay(23).withMinuteOfHour(30).withSecondOfMinute(0).withMillisOfSecond(0);
	}


	public static DateTime time22_00(DateTime time) {
		return time.withHourOfDay(22).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}



	public static DateTime time08_00(DateTime time) {
		return time.withHourOfDay(8).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time03_00(DateTime time) {
		return time.withHourOfDay(3).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time11_55(DateTime time) {
		return time.withHourOfDay(11).withMinuteOfHour(55).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time13_05(DateTime time) {
		return time.withHourOfDay(13).withMinuteOfHour(5).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time17_55(DateTime time) {
		return time.withHourOfDay(17).withMinuteOfHour(55).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time19_05(DateTime time) {
		return time.withHourOfDay(19).withMinuteOfHour(5).withSecondOfMinute(0).withMillisOfSecond(0);
	}


	public static DateTime time12_00(DateTime time) {
		return time.withHourOfDay(12).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time13_00(DateTime time) {
		return time.withHourOfDay(13).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time20_00(DateTime time) {
		return time.withHourOfDay(20).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}

	public static DateTime time21_00(DateTime time) {
		return time.withHourOfDay(21).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
	}


	/**
	 * 两天是否是同一天
	 * 
	 * @return
	 */
	public static boolean isSameDay(DateTime dt1, DateTime dt2) {
		DateTime dt1temp = dt1.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
		DateTime dt2temp = dt2.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);

		return Days.daysBetween(dt1temp, dt2temp).getDays() == 0;
	}
	
	/**
	 * 两天是否是同一天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date1);
		int d1 = cal1.get(Calendar.DAY_OF_YEAR);
		int y1 = cal1.get(Calendar.YEAR);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(date2);
		int d2 = cal2.get(Calendar.DAY_OF_YEAR);
		int y2 = cal2.get(Calendar.YEAR);

		return d1 == d2 && y1 == y2;
	}

	public static boolean isToday(Date date) {
		if (date == null)
			return false;

		Calendar cal1 = Calendar.getInstance();
		cal1.setTime(date);
		int d1 = cal1.get(Calendar.DAY_OF_YEAR);
		int y1 = cal1.get(Calendar.YEAR);

		Calendar cal2 = Calendar.getInstance();
		cal2.setTime(getNowDate());
		int d2 = cal2.get(Calendar.DAY_OF_YEAR);
		int y2 = cal2.get(Calendar.YEAR);

		return d1 == d2 && y1 == y2;
	}

	public static String getDateStrYMD(Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.format(date);
	}

	public static String getDateStrYMDHMS(Date date) {
		if (date == null)
			return "0";

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return simpleDateFormat.format(date);
	}

	public static Date getString2Date(String str) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			return simpleDateFormat.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String now(String df) {
		return new SimpleDateFormat(df).format(new Date());
	}

	public static long getDelayTime(int hour, int min, int sec, long peroid) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, min);
		calendar.set(Calendar.SECOND, sec);
		calendar.set(Calendar.MILLISECOND, 0);
		Date date = calendar.getTime();

		long time = System.currentTimeMillis();

		long temp = date.getTime() - time;
		temp = temp < 0 ? temp + peroid : temp;
		return temp;
	}

	public static long getDelayTime(int weekday, int hour, int min, int sec, long peroid) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_WEEK, weekday);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, min);
		calendar.set(Calendar.SECOND, sec);
		calendar.set(Calendar.MILLISECOND, 0);
		Date date = calendar.getTime();

		long time = System.currentTimeMillis();

		long temp = time - date.getTime();
		temp = temp < 0 ? temp + peroid : temp;
		return temp;
	}

	public static Calendar getCalendar(long time) {
		Date date = new Date(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}

	public static Calendar getCalendar(int hour, int min, int sec) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, min);
		calendar.set(Calendar.SECOND, sec);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar;
	}

	//TODO: main
//	public static void main(String[] args) {
//	
//		DateTime old = new DateTime(Long.valueOf("1635488991000"));
//		DateTime now = DateTime.now();
//		System.out.println(days(old, now));
////		System.out.println(getWeekEnd(now));
////		System.out.println(getMonthBegin(now));
////		System.out.println(getMonthEnd(now));
//	}

	public static boolean isSameMonth(long time, long curtime) {
		Calendar instance = Calendar.getInstance();
		instance.setTimeInMillis(time);
		int m1 = instance.get(Calendar.MONTH);
		int y1 = instance.get(Calendar.YEAR);

		instance.setTimeInMillis(curtime);
		int m2 = instance.get(Calendar.MONTH);
		int y2 = instance.get(Calendar.YEAR);
		return y1 == y2 && m1 == m2;
	}

	public static String getDateString(long time) {
		if (time <= 0) {
			return "";
		}

		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String temp = sdf.format(date);
		return temp;
	}

	public static DateTime getDateTime(String datestr) {
		DateTime dt = null;
		try {
			DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
			// 时间解析
			dt = DateTime.parse(datestr, format);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return dt;
	}

	public static String getTimeString(long time) {
		time = time / 1000;
		String timeStr = null;
		long hour = 0;
		long minute = 0;
		long second = 0;
		if (time <= 0)
			return "00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = "00" + ":" + unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	public static String unitFormat(long i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Long.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	public static String getFormat(String formate, long time) {
		if (time <= 0) {
			return "";
		}

		Date date = new Date(time);
		SimpleDateFormat sdf = new SimpleDateFormat(formate);
		String temp = sdf.format(date);
		return temp;
	}

	//本周开始时间
	public static DateTime getWeekBegin(DateTime time) {
		return time.minusDays(new DateTime().getDayOfWeek()-1).withTimeAtStartOfDay();
	}
	
	//本周结束时间
	public static DateTime getWeekEnd(DateTime time) {
		return time.plusDays(8-new DateTime().getDayOfWeek()).withTimeAtStartOfDay();
	}
	
	//本月开始时间
	public static DateTime getMonthBegin(DateTime time) {
		return time.minusDays(new DateTime().getDayOfMonth()-1).withTimeAtStartOfDay();
	}
	
	//本月开始时间
	public static DateTime getMonthEnd(DateTime time) {
		return time.minusDays(new DateTime().getDayOfMonth()-1).plusMonths(1).withTimeAtStartOfDay();
	}


	/**
	 * 获取两个日期相差的月数
	 */
	public static int getMonthDiff(Date d1, Date d2) {
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		int year1 = c1.get(Calendar.YEAR);
		int year2 = c2.get(Calendar.YEAR);
		int month1 = c1.get(Calendar.MONTH);
		int month2 = c2.get(Calendar.MONTH);
		int day1 = c1.get(Calendar.DAY_OF_MONTH);
		int day2 = c2.get(Calendar.DAY_OF_MONTH);
		// 获取年的差值?
		int yearInterval = year1 - year2;
		// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
		if (month1 < month2 || month1 == month2 && day1 < day2) {
			yearInterval--;
		}
		// 获取月数差值
		int monthInterval = (month1 + 12) - month2;
		if (day1 < day2) {
			monthInterval--;
		}
		monthInterval %= 12;
		int monthsDiff = Math.abs(yearInterval * 12 + monthInterval);
		return monthsDiff;
	}

	public static void main(String[] args) {
        DateTime now = DateTime.now();
        DateTime monthBegin = getMonthBegin(now);
        System.out.println(now);
        System.out.println(monthBegin);

    }
}
